function status = ctlFileEdit(fileInit,fileFin,Opts)
%ctlFileEdit - editing a .ctl file (Hector control file)
%
%   STATUS = ctlFileEdit(fileInit,fileFin,Opts)
%
% This function allows the editing of a .ctl file fileInit, leading to
% the final .ctl file fileFin, on the basis of the fields and values 
% of the struct variable Opts, where the extension .ctl designates the
% ASCII control files of Hector, which is a GNSS data processing package.
% Each row of a .ctl file contains a command wicth a field/value style.
% If particular,
%   - if a field in fileInit is also in Opts and the value in Opts is 
%     non-empty, the value in Opts is used for this field in fileFin;
%   - if a field in fileInit is not in Opts, the couple field/value in
%     this file is directly passed to fileFin;
%   - if a field in fileInit is also in Opts and the value in Opts is 
%     empty, the corresponding couple field/value is not passed to 
%     fileFin, i.e. the row is removed;
%   - if fileInit is empty or the file fileInit does not exist, all and
%     only field/values in Opts are passed to fileFin.
%
% If fileInit (fileFin) is an extensionless file, the extension '.ctl' is
% automatically added.
%
% The output STATUS is true if the process is successful (fileFin is
% written), false elsewere. 
%
%   STATUS = ctlFileEdit(fileInit,fileFin,Opts)

% G. Teza, 2021

if ~isempty(fileInit)
    [pathInit,nameInit,extInit] = fileparts(fileInit);
    if isempty(extInit)
        fileInit = fullfile(pathInit,[nameInit '.ctl']);
    end
    if exist(fileInit,'file')
        fidInit = fopen(fileInit);
        txtIn = textscan(fidInit,'%s','delimiter','\t');
        fclose(fidInit);
    else
        txtIn = [];
    end
else
    txtIn = [];
end

Optfn = fieldnames(Opts);
nfn = numel(Optfn);

if ~isempty(txtIn)
    cIn = txtIn{:};     % input cell array
    nr = size(cIn,1);
    % generation of 2-columns input cell array: 
    cIn2 = cell(nr,2);  % initialization  
    for k = 1:nr
        rk = cIn{k};
        rkc = strsplit(rk);
        if numel(rkc) > 2   % for the cases where there are more than one value  
            rkc2 = strjoin(rkc(2:end),' ');  % and a single string must be generated
            rkcn = cell(1,2); 
            rkcn(1) = rkc(1);   
            rkcn(2) = {rkc2};   % rkc2 is a string and rkcn is a cell variable  
            rkc = rkcn;
        end
        cIn2(k,:) = rkc;
    end
    
    cOut2 = cIn2;

    Ie = zeros(nfn,1);  % initialization of vector of valid Opts elements
    Ir = zeros(nr,1);   % initializaion of vector of file rows to be deleted  
    for kk = 1:nfn
        fnamekk = Optfn{kk};
        for k = 1:nr
            varnamek = cIn2{k,1};
            if strcmpi(fnamekk,varnamek)
                valkk = Opts.(fnamekk);
                if ~isempty(valkk)
                    cOut2(k,2) = {valkk};
                else
                    Ir(k) = 1;
                end
                Ie(kk) = 1;
            end
        end
    end

    Ir = logical(Ir);
    if sum(Ir) > 0  % at least a row must be deleted
        cOut2(Ir,:) = [];
    end

else
    
    cOut2 = [];
    Ie = false(nfn,1);    
    
end

neok = sum(Ie);     % actually used Opts elements
if neok < nfn        
    neincr = nfn-neok;  % Opts fields which are not in fileInit
    cOut2incr = cell(neincr,2); % incremental cOut2 initialization
    Iok = false(neincr,1);      % init variable aimed at avoiding problems if fileInit is empty 
    n = 1;
    for kk = 1:nfn
        if ~Ie(kk)
            fnamekk = Optfn{kk};
            valkk = Opts.(fnamekk);
            if ~isempty(valkk)
                cOut2incr(n,1) = Optfn(kk);
                cOut2incr(n,2) = {valkk};
                Iok(n) = true;
            end
            n = n+1;
        end
    end
    if sum(Iok) < nfn
        cOut2incr(~Iok,:) = [];
    end
    cOut2 = [cOut2; cOut2incr];
end

[pathFin,nameFin,extFin] = fileparts(fileFin);
if ~isempty(pathFin) && (exist(pathFin,'dir') ~=7)
    mkdir(pathFin)
end
if isempty(extFin)
    fileFin = fullfile(pathFin,[nameFin '.ctl']);
end

try
    writecell(cOut2,fileFin,'FileType','text','delimiter','tab');
    status = true;
catch
    status = false;
end